package com.bls.sms.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.bls.sms.dao.interfaces.SubjectDAO;
import com.bls.sms.exception.ApplicationException;
import com.bls.sms.wrapper.Subject;

public class SubjectDAOImpl implements SubjectDAO {
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public int saveSubject(Subject subject) throws ApplicationException {
		try {
			this.sessionFactory.getCurrentSession().beginTransaction();
			this.sessionFactory.getCurrentSession().save(subject);
			this.sessionFactory.getCurrentSession().getTransaction().commit();
		} catch (HibernateException e) {
			this.sessionFactory.getCurrentSession().getTransaction().rollback();
			throw new ApplicationException(e.getMessage());
		}
		return 1;
	}

	public int updateSubject(Subject subject) throws ApplicationException {
		try {
			this.sessionFactory.getCurrentSession().beginTransaction();

			// Subject subject =
			// this.sessionFactory.getCurrentSession().get(Subject.class, new
			// Long(1));

			this.sessionFactory.getCurrentSession().update(subject);
			this.sessionFactory.getCurrentSession().getTransaction().commit();
		} catch (HibernateException e) {
			this.sessionFactory.getCurrentSession().getTransaction().rollback();
			throw new ApplicationException(e.getMessage());
		}
		return 1;
	}

	public int deleteSubject(int subjectId) throws ApplicationException {
		try {
			this.sessionFactory.getCurrentSession().beginTransaction();
			this.sessionFactory.getCurrentSession().createQuery(
					"delete from Subject subject where id = " + subjectId)
					.executeUpdate();
			this.sessionFactory.getCurrentSession().getTransaction().commit();
		} catch (HibernateException e) {
			this.sessionFactory.getCurrentSession().getTransaction().rollback();
			throw new ApplicationException(e.getMessage());
		}
		return 1;
	}

	public List<Subject> loadSubjects() throws ApplicationException {
		try {
			this.sessionFactory.getCurrentSession().beginTransaction();
			return this.sessionFactory.getCurrentSession().createQuery(
					"from Subject subject ").list();
		} catch (HibernateException e) {
			throw new ApplicationException(e.getMessage());
		}
	}

	public Subject getSubjectDetails(int subjectId) throws ApplicationException {
		try {
			Subject subject = (Subject) this.sessionFactory.getCurrentSession()
					.get(Subject.class, new Integer(subjectId));
			return subject;
		} catch (HibernateException e) {
			throw new ApplicationException(e.getMessage());
		}
	}

	public int getMaxId() throws ApplicationException {
		try {
			this.sessionFactory.getCurrentSession().beginTransaction();
			Query query = this.sessionFactory.getCurrentSession().createQuery(
					"select max(id) from Subject subject ");
			List list = query.list();
			int maxId = (Integer) list.get(0);
			return maxId;
		} catch (HibernateException e) {
			throw new ApplicationException(e.getMessage());
		}
	}
}
